package com.zzg.springboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzg.springboot.dao.ProductDao;
import com.zzg.springboot.dao.ProductTypeDao;
import com.zzg.springboot.entity.Product;
import com.zzg.springboot.entity.ProductType;

@Controller
@RequestMapping("/api/springboot")
public class ProductController {
	@Autowired
	private ProductDao productDao;
	@Autowired
	private ProductTypeDao productTypeDao;

	@RequestMapping("/one")
	public void one() {
		ProductType type = new ProductType();
		type.setName("产品类型1");

		// 传入type的本质是处理数据库product表的type_id外键
		Product product1 = new Product("产品1", null);
		Product product2 = new Product("产品2", type);
		
		productTypeDao.save(type);
		productDao.save(product1);
		productDao.save(product2);
	}
	
	@RequestMapping("/two")
	@ResponseBody
	public List<Product> two(){
		List<Product> list = productDao.findAll();
		return list;
	}

}
